var wizard = null;
var myDropzone = null;
jQuery(function($) {
	 $("#previews").resize(function(){
	 });
	 Dropzone.autoDiscover = false;
	var url = window.global.getFinalUrl(window.global.ctx+"/sys/file/upload?dir="+$("#saveDir").val()+"&pId="+$("#pId").val()); 
	
	 // Get the template HTML and remove it from the doument
    var previewNode = document.querySelector("#template");
    previewNode.id = "";
    previewNode.style.display="";
    var previewTemplate = previewNode.parentNode.innerHTML;
    previewNode.parentNode.removeChild(previewNode);
    var extInfo = null;
    $("#extInfo").text(extInfo);
    var maxFiles = 5;
    $("#maxFiles").text(maxFiles);
    var maxFilesize = 50;
    $("#maxFilesize").text(maxFilesize+" MB");
    
	myDropzone = new Dropzone("#uploadFile", { // Make the whole body a dropzone
	        url: url, // Set the url
	        thumbnailWidth: 80,
	        thumbnailHeight: 80,
	        parallelUploads: 20,
	        maxFilesize:50,
	        maxFiles:5,
	        dictInvalidFileType: "不支持该类型的文件。",
	        dictCancelUploadConfirmation: "确定取消上传该文件?",
	        dictResponseError:"服务器返回{{statusCode}}。",
	        dictRemoveFileConfirmation:"确定删除该文件？",
	        dictMaxFilesExceeded: "您最多只能上传{{maxFiles}}个文件！",
	        dictResponseError: '文件上传失败!',
	        dictInvalidFileType: "你不能上传该类型文件,文件类型只能是*.xls,*.xlsx。",
	        dictFallbackMessage:"浏览器不受支持",
	        dictFileTooBig:"文件过大({{filesize}}MiB),上传文件最大支持{{maxFilesize}}MiB.",
	        acceptedFiles:extInfo,
	        previewTemplate: previewTemplate,
	        autoQueue: false, // Make sure the files aren't queued until manually added
	        previewsContainer: "#previews", // Define the container to display the previews
	        clickable: ".fileinput-button" // Define the element that should be used as click trigger to select files.
	      });
	  	  myDropzone.data = new Map();
	      myDropzone.on("addedfile", function(file) {
	        // Hookup the start button
	    	  $("#previews").resize();
	          file.previewElement.querySelector(".start").onclick = function() { myDropzone.enqueueFile(file); };
	      });

	      // Update the total progress bar
	      myDropzone.on("totaluploadprogress", function(progress) {
	        document.querySelector("#total-progress .progress-bar").style.width = progress + "%";
	      });

	      myDropzone.on("sending", function(file) {
	        // Show the total progress bar when upload starts
	        document.querySelector("#total-progress").style.opacity = "1";
	        // And disable the start button
	        file.previewElement.querySelector(".start").setAttribute("disabled", "disabled");
	      });

	      // Hide the total progress bar when nothing's uploading anymore
	      myDropzone.on("queuecomplete", function(progress) {
	        document.querySelector("#total-progress").style.opacity = "0";
	      });
	      myDropzone.on("success", function(file, responseText, e) {
	    	  console.log("success:")
	    	  console.log(file)
	    	  console.log(responseText)
	    	  file.deleteUrl = responseText.data.success[0].deleteUrl;
	    	  setTimeout(function(){
	    		    file.autodel = true;
	    		    myDropzone.removeFile(file);
	    	  },2000);
	      });
	      myDropzone.on("canceled", function(file) {
	    	  console.log("canceled:")
	    	  console.log(file)
		  });
	      myDropzone.on("removedfile", function(file) {
	    	  console.log("removedfile:")
	    	  console.log(file);
	    	  if(!file.autodel){
	    		  $.get(window.global.getFinalUrl(file.deleteUrl),null,function(result){
	    			  if(result&& result.status==200){
	    			  }
	    		  });
	    	  }
	      });
	      
});

